package com.itmk.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itmk.resp.Payload;
import io.jsonwebtoken.*;

import java.util.*;

public class JWTUtils {
    private static long time = 1000*60*60*24*7;
    private static String sign = "admin";

    //.sign(Algorithm.HMAC256(SECRET));

    //创建JWT令牌（新）
    public static String createJwt(Payload payload){
        JwtBuilder jwtBuilder = Jwts.builder();
        String uuid = payload.getUuid();
        String phone = payload.getPhone();
        String JwtToken  =jwtBuilder
                //头部
                .setHeaderParam("type","JWT")
                .setHeaderParam("alg","HS256")
                //载荷
                .claim("uuid",uuid)
                .claim("phone",phone)
                .setSubject("admin-test")
                .setExpiration(new Date(System.currentTimeMillis()+time))//token过期时间
                .setId(UUID.randomUUID().toString())
                //签名
                .signWith(SignatureAlgorithm.HS256,sign)//设置加密算法
                //使用
                .compact();
        return JwtToken;
    }

    //验证token是否过期
    public static boolean checkToken(String token){
        if (token == null || token == "") {
            return false;
        }
       try{
           Jws<Claims> claimsJws = Jwts.parser().setSigningKey(sign).parseClaimsJws(token);
       }catch (Exception e){
           e.printStackTrace();
           return false;
       }
        return true;
    }


    //获取sign值
    public static String getSign(){
        return sign;
    }


}
